import { createPinia } from "pinia";
import Date_utils from "@/utils/Date_format";
import { useRoute, useRouter } from "vue-router";
const pinia = createPinia();

//全局函数及方法
pinia.use(({ store }) => {
  store.Router = useRouter();
  store.Route = useRoute();
  store.D = Date_utils;
  
  // 从 sessionStorage 获取菜单状态
  const menuData = sessionStorage.getItem("_menu");
  const defaultMenu = menuData ? JSON.parse(menuData) : { title: "索赔单", url: "/claim/home", idx: 0 };
  
  //菜单处理
  store.M = {
    title: defaultMenu.title,
    url: defaultMenu.url,
    idx: defaultMenu.idx,
    Push(data) {
      const { title, url, idx } = data;
      sessionStorage.setItem("_menu", JSON.stringify(data));
      store.M.title = title;
      store.M.url = url;
      store.M.idx = idx;
      store.Router.push(url);
    },
    To() {
      store.Router.go(0);
    },
    GetId() {
     return store.Route.params.id || store.Route.query.id;
    },
  };
  store.G = {
    // 这里可以添加全局变量
    baseURL: "http://api.example.com",
    version: "1.0.0",
    // 全局方法
    formatDate: (date) => {
      return new Date(date).toLocaleDateString();
    },
  };
});

export default pinia;
